<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Categories extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->loadData('controllerName',$this->router->fetch_class());
	}
	
	public function index(){
		redirect('administrator/categories/typechannel');
	}
	
	//Quan ly loai kenh
	public function typechannel(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->loadScript(base_url('js/jquery.validate.js'));
		$this->loadScript(base_url('js/localization/messages_vi.js'));
		$this->loadScript(base_url('js/admin/jquery.modify.js'));
		$this->loadScript(base_url('js/admin/jquery.update.status.js'));
		$this->headTitle('Quản lý người dùng - Admin Panel');
		
		$this->load->model('TypeChannel_Model');
		$counttypechannel = $this->TypeChannel_Model->countTypeChannel();
		$this->loadData('counttypechannel',$counttypechannel);
		$this->loadData('listtypechannel', $this->TypeChannel_Model->getTypeChannel());
			
		if($this->input->post()){
			if($this->form_validation->run('formsearch') == true){
				$keysearch = trim($this->input->post('keysearch', true));
				$this->loadData('textkeysearch', $keysearch);
				$this->loadData('listtypechannel', $this->TypeChannel_Model->searchTypeChannel($keysearch));
			}

		}
		$this->loadView('administrator/categories/typechannel');
	
	}
	//Them loai kenh
	public function add_typechannel(){
		$this->load->helper('form');
		$this->load->library('form_validation');	
		$this->load->model('TypeChannel_Model');
		if($this->input->post()){
			if($this->form_validation->run('typechannel') == true){
				$this->TypeChannel_Model->insertTypeChannel();
				redirect('administrator/categories/typechannel');	
			}		
		}
		$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
		$this->headTitle('Thêm loại kênh - Admin Panel');
		$this->loadView('administrator/categories/add_typechannel');

	}
	
	//Sua loai kenh
	public function modify_typechannel(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('TypeChannel_Model');	
		$tcid = $this->input->get('tcid', true);
		$typechannel = $this->TypeChannel_Model->getTypeChannelByTcId($tcid);
		if($typechannel){
			if($this->input->post()){
				if($this->form_validation->run('typechannel') == true){			
					$this->TypeChannel_Model->updateTypeChannel($tcid);
					redirect('administrator/categories/typechannel');
				}		
			}
			$this->loadData('typechannel', $typechannel);
			$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
			$this->headTitle('Sửa loại kênh - Admin Panel');
			$this->loadView('administrator/categories/modify_typechannel');
		}else{
			redirect('administrator/categories/typechannel');
		}
	}
	//Doi trang thai loai kenh
	public function change_status_typechannel(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$status = $this->input->post('status', true);
			$this->load->model('TypeChannel_Model');
			$changeStatus = $this->TypeChannel_Model->changeStatusTypeChannel($key, $status);
			if($changeStatus){
				$json['status'] = $changeStatus;
				$json['success'] = true;
			}else{
				$json['success'] = false;
			}
			echo json_encode($json);
		}
	}
	
	//Xoa loai kenh
	public function delete_typechannel(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$this->load->model('TypeChannel_Model');		
			if($this->TypeChannel_Model->deleteTypeChannel($key)){
				$json["success"] = true;
			}else{
				$json["success"] = false;
			}
			echo json_encode($json);
		}
	}
	
}